<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px" class="query-params-form">
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织" prop="organization">
        <el-input
          v-model="queryParams.organization"
          placeholder="请输入组织"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入部门"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工" prop="employeeName">
        <el-input
          v-model="queryParams.employeeName"
          placeholder="请输入员工"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入岗位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工位" prop="workstation">
        <el-input
          v-model="queryParams.workstation"
          placeholder="请输入工位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓管员" prop="warehouseManager">
        <el-input
          v-model="queryParams.warehouseManager"
          placeholder="请输入仓管员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据状态" prop="dataStatus">
        <el-input
          v-model="queryParams.dataStatus"
          placeholder="请输入数据状态"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="禁用状态" prop="isDisabled">
        <el-select v-model="queryParams.isDisabled" placeholder="请选择禁用状态" clearable>
          <el-option
            v-for="dict in disabled_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['barcode:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['barcode:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['barcode:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['barcode:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工号" align="center" prop="employeeId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="组织" align="center" prop="organization" />
      <el-table-column label="部门" align="center" prop="department" />
      <el-table-column label="员工" align="center" prop="employeeName" />
      <el-table-column label="岗位" align="center" prop="position" />
      <el-table-column label="工位" align="center" prop="workstation" />
      <el-table-column label="仓管员" align="center" prop="warehouseManager" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="字段映射标识" align="center" prop="fieldMappingIdentifier" />
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="创建日期" align="center" prop="createdDate" />
      <el-table-column label="数据状态" align="center" prop="dataStatus" />
      <el-table-column label="禁用状态" align="center" prop="isDisabled">
        <template #default="scope">
          <dict-tag :options="disabled_status" :value="scope.row.isDisabled"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['barcode:user:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['barcode:user:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改条码用户对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="userRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="passwordHash">
          <el-input v-model="form.passwordHash" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="组织" prop="organization">
          <el-input v-model="form.organization" placeholder="请输入组织" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-input v-model="form.department" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="员工" prop="employeeName">
          <el-input v-model="form.employeeName" placeholder="请输入员工" />
        </el-form-item>
        <el-form-item label="岗位" prop="position">
          <el-input v-model="form.position" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item label="工位" prop="workstation">
          <el-input v-model="form.workstation" placeholder="请输入工位" />
        </el-form-item>
        <el-form-item label="仓管员" prop="warehouseManager">
          <el-input v-model="form.warehouseManager" placeholder="请输入仓管员" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="字段映射标识" prop="fieldMappingIdentifier">
          <el-input v-model="form.fieldMappingIdentifier" placeholder="请输入字段映射标识" />
        </el-form-item>
        <el-form-item label="数据状态" prop="dataStatus">
          <el-input v-model="form.dataStatus" placeholder="请输入数据状态" />
        </el-form-item>
        <el-form-item label="禁用状态" prop="isDisabled">
          <el-select v-model="form.isDisabled" placeholder="请选择禁用状态">
            <el-option
              v-for="dict in disabled_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位编码Id" prop="positionCodeId">
          <el-input v-model="form.positionCodeId" placeholder="请输入岗位编码Id" />
        </el-form-item>
        <el-form-item label="岗位名称" prop="positionName">
          <el-input v-model="form.positionName" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="组织编码Id" prop="organizationCodeId">
          <el-input v-model="form.organizationCodeId" placeholder="请输入组织编码Id" />
        </el-form-item>
        <el-form-item label="组织名称" prop="organizationName">
          <el-input v-model="form.organizationName" placeholder="请输入组织名称" />
        </el-form-item>
        <el-form-item label="员工编码Id" prop="employeeCodeId">
          <el-input v-model="form.employeeCodeId" placeholder="请输入员工编码Id" />
        </el-form-item>
        <el-form-item label="员工名称" prop="employeeFullName">
          <el-input v-model="form.employeeFullName" placeholder="请输入员工名称" />
        </el-form-item>
        <el-form-item label="部门编码Id" prop="departmentCodeId">
          <el-input v-model="form.departmentCodeId" placeholder="请输入部门编码Id" />
        </el-form-item>
        <el-form-item label="部门名称" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="工位编码Id" prop="workstationCodeId">
          <el-input v-model="form.workstationCodeId" placeholder="请输入工位编码Id" />
        </el-form-item>
        <el-form-item label="工位名称" prop="workstationName">
          <el-input v-model="form.workstationName" placeholder="请输入工位名称" />
        </el-form-item>
        <el-form-item label="仓管员Id" prop="warehouseManagerId">
          <el-input v-model="form.warehouseManagerId" placeholder="请输入仓管员Id" />
        </el-form-item>
        <el-form-item label="仓管员名称" prop="warehouseManagerName">
          <el-input v-model="form.warehouseManagerName" placeholder="请输入仓管员名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="User">
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/barcode/user";

const { proxy } = getCurrentInstance();
const { disabled_status } = proxy.useDict('disabled_status');

const userList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    organization: null,
    department: null,
    employeeName: null,
    position: null,
    workstation: null,
    warehouseManager: null,
    description: null,
    dataStatus: null,
    isDisabled: null,
  },
  rules: {
    employeeId: [
      { required: true, message: "工号不能为空", trigger: "blur" }
    ],
    userName: [
      { required: true, message: "用户名不能为空", trigger: "blur" }
    ],
    passwordHash: [
      { required: true, message: "密码不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询条码用户列表 */
function getList() {
  loading.value = true;
  listUser(queryParams.value).then(response => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    employeeId: null,
    userName: null,
    passwordHash: null,
    organization: null,
    department: null,
    employeeName: null,
    position: null,
    workstation: null,
    warehouseManager: null,
    description: null,
    fieldMappingIdentifier: null,
    createdBy: null,
    createdDate: null,
    modifiedBy: null,
    modifiedDate: null,
    dataStatus: null,
    isDisabled: null,
    positionCodeId: null,
    positionName: null,
    organizationCodeId: null,
    organizationName: null,
    employeeCodeId: null,
    employeeFullName: null,
    departmentCodeId: null,
    departmentName: null,
    workstationCodeId: null,
    workstationName: null,
    warehouseManagerId: null,
    warehouseManagerName: null
  };
  proxy.resetForm("userRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.employeeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加条码用户";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _employeeId = row.employeeId || ids.value
  getUser(_employeeId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改条码用户";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["userRef"].validate(valid => {
    if (valid) {
      if (form.value.employeeId != null) {
        updateUser(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addUser(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _employeeIds = row.employeeId || ids.value;
  proxy.$modal.confirm('是否确认删除条码用户编号为"' + _employeeIds + '"的数据项？').then(function() {
    return delUser(_employeeIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('barcode/user/export', {
    ...queryParams.value
  }, `user_${new Date().getTime()}.xlsx`)
}

getList();
</script>
